
import 'package:flutter/material.dart';
import 'package:freedom_flutter_mall/views/screens/cart/cart_screen.dart';
import 'package:freedom_flutter_mall/views/screens/category/category_screen.dart';
import 'package:freedom_flutter_mall/views/screens/home/home_screen.dart';
import 'package:freedom_flutter_mall/views/screens/live/live_screen.dart';
import 'package:freedom_flutter_mall/views/screens/mine/mine_screen.dart';
import 'package:freedom_flutter_mall/views/widgets/load_image.dart';

class MainTabScreens extends StatefulWidget {
  const MainTabScreens({super.key});

  @override
  State<MainTabScreens> createState() => _MainTabScreensState();
}

class _MainTabScreensState extends State<MainTabScreens> {

  int _select = 0;

  PageController pageController = PageController();

  GlobalKey homeKey = GlobalKey();
  GlobalKey categoryKey = GlobalKey();
  GlobalKey liveKey = GlobalKey();
  GlobalKey cartKey = GlobalKey();
  GlobalKey profileKey = GlobalKey();

  static LoadAssetImage generateIcon(String path) {
    return LoadAssetImage('tabbar/$path',
      width: 24,
      height: 24,
    );
  }

  final List<BottomNavigationBarItem> items = [
    BottomNavigationBarItem(
      icon: generateIcon('light/Home'),
      activeIcon: generateIcon('dark/Home'),
      label: '首页',
    ),
    BottomNavigationBarItem(
      icon: generateIcon('light/Bag'),
      activeIcon: generateIcon('dark/Bag'),
      label: '分类',
    ),
    BottomNavigationBarItem(
      icon: generateIcon('light/Bag'),
      activeIcon: generateIcon('dark/Bag'),
      label: '购物车',
    ),
    BottomNavigationBarItem(
      icon: generateIcon('light/Wallet'),
      activeIcon: generateIcon('dark/Wallet'),
      label: '钱包',
    ),
    BottomNavigationBarItem(
      icon: generateIcon('light/Profile'),
      activeIcon: generateIcon('dark/Profile'),
      label: '我的',
    ),

  ];

  @override
  void initState() {
    super.initState();
    //初始化数据
    _initData();
  }

  @override
  void dispose() {
    super.dispose();
    pageController.dispose();
  }

  _initData() async {
    // Future.delayed(Duration.zero,() {
    //   context.read<MemberProvider>().requestMemberInfo();
    // });
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _buildItemPage(),
      bottomNavigationBar: BottomNavigationBar(
        items: items,
        onTap: (index) {
          pageController.jumpToPage(index);

          setState(() {
            _select = index;
          });
        },
        currentIndex: _select,
        selectedLabelStyle: const TextStyle(
          fontWeight: FontWeight.bold,
          fontSize: 10,
        ),
        showUnselectedLabels: true,
        unselectedLabelStyle: const TextStyle(
          fontWeight: FontWeight.normal,
          fontSize: 10,
        ),
        selectedItemColor: const Color(0xFF212121),
        unselectedItemColor: const Color(0xFF9E9E9E),
      ),
    );
  }

  Widget _buildItemPage() {
    return PageView.builder(
      itemCount: 5,
      physics: const NeverScrollableScrollPhysics(),
      controller: pageController,
      itemBuilder: (context, index) {
        if (index == 0) {
          return HomeScreen(key: homeKey,);
        } else if (index == 1) {
          return CategoryScreen(key: categoryKey,);
        } else if (index == 2) {
          return LiveScreen(key: liveKey,);
        } else if (index == 3) {
          return CartScreen(key: cartKey,);
        } else if (index == 4) {
          return MineScreen(key: profileKey,);
        }
        return const Text("页面未定义");
      },
    );
  }
}
